var startToShowColumns = 0;

function showAll(event) {
	for ( var i = startToShowColumns, iLen = dt_config_aoColumns.length; i < iLen; i++) {
		oTable.fnSetColumnVis(i, true);
	}
	updateVisibilityIcon();
	event.stopPropagation();
}

function showOnlyCampaignColumns(event) {
	for ( var i = startToShowColumns, iLen = dt_config_aoColumns.length; i < iLen; i++) {
		oTable.fnSetColumnVis(i, i < 12);
		
	}	
	updateVisibilityIcon();
	event.stopPropagation();
}

function showOnlyPromocodeColumns(event) {
	for ( var i = startToShowColumns, iLen = dt_config_aoColumns.length; i < iLen; i++) {
		oTable.fnSetColumnVis(i, i >= 11);
	}
	
	updateVisibilityIcon();
	event.stopPropagation();
}

function updateVisibilityIcon(){
	$.each($('#columnsVisibleConfigurator li i'), function(index, singleVisibilityIcon) {
		var currentState = oTable.fnSettings().aoColumns[index].bVisible;
		$(singleVisibilityIcon).toggleClass("icon-ok", currentState);	
	});
	
}

function setColumnVisibility(index, thizz, event) {
	var currentColumn = oTable.fnSettings().aoColumns[index];
	var nextState = (currentColumn.bVisible == undefined || currentColumn.bVisible == false);
	oTable.fnSetColumnVis(index, nextState);
	console.log(currentColumn.mData + "_" + nextState);
	$(thizz).siblings().toggleClass("icon-ok", nextState);
	event.stopPropagation();
}

$(document).ready(
		function() {
			
			// add all columns to enable/disable to the dropdown
			var columns = oTable.fnSettings().aoColumns;
			 var columnsVisibilitySelection = '<li class="divider"></li>';
			$.each(columns, function(index, value) {
				var icon = (value.bVisible ? 'class="icon-ok"' : '');
				
				columnsVisibilitySelection += '<li><a style="display:inline" href="#" onclick="setColumnVisibility(' +(index)+ ', this, event);" data-stopPropagation="true">'
				+ value.mData + '</a><i ' +icon+ '></i></li>';
			});
			
			$('#columnsVisibleConfigurator').append(columnsVisibilitySelection);
		});
